package com.ysx.server.service.impl;
import java.time.LocalDateTime;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ysx.server.mapper.GoodsMapper;
import com.ysx.server.mapper.ReportMapper;
import com.ysx.server.mapper.UserMapper;
import com.ysx.server.pojo.dto.ReportDTO;
import com.ysx.server.pojo.dto.ReportPageQueryDTO;
import com.ysx.server.pojo.entity.Goods;
import com.ysx.server.pojo.entity.Report;
import com.ysx.server.pojo.entity.User;
import com.ysx.server.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 举报分页查询
     *
     * @param reportPageQueryDTO
     * @return
     */
    @Override
    public Page<Report> page(ReportPageQueryDTO reportPageQueryDTO) {
        PageHelper.startPage(reportPageQueryDTO.getCurrent(),reportPageQueryDTO.getPageSize());
        return reportMapper.page(reportPageQueryDTO);
    }

    /**
     * 修改举报信息
     *
     * @param report
     * @return
     */
    @Override
    public boolean update(Report report) {
        if(report==null){
            return false;
        }
        return reportMapper.update(report);
    }

    /**
     * 删除举报
     *
     * @param id
     * @return
     */
    @Override
    public boolean delComment(Long id) {
        if(id==null||id<0){
            return false;
        }
        return reportMapper.deleteById(id);
    }

    /**
     * 保存举报信息
     *
     * @param reportDTO
     */
    @Override
    public void saveReport(ReportDTO reportDTO) {
        if(reportDTO==null){
            return;
        }
        User user = userMapper.getById(reportDTO.getUserId());
        Goods goods = goodsMapper.getById(reportDTO.getGoodsId());
        Report report = new Report();
        report.setGoodsId(reportDTO.getGoodsId());
        report.setGoodsName(goods.getGoodsName());
        report.setUserId(reportDTO.getUserId());
        report.setUsername(user.getUsername());
        report.setReportDescription(reportDTO.getMsg());
        reportMapper.saveReport(report);
    }
}
